package com.css.fxfzysx.modules.vaolEngineering.repository.impl;

import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxAirReceiverEntity;
import com.css.fxfzysx.modules.lifeline.vo.basic.DestructiveStateVo;
import com.css.fxfzysx.modules.vaolEngineering.param.update.AirSupplyParam;
import com.css.fxfzysx.modules.vaolEngineering.repository.SgManagementRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class SgManagementRepositoryImpl implements SgManagementRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public boolean saveAirReceiver(SmxAirReceiverEntity entity) {
        String sql = "insert into czt_smx_air_receiver values (:id,:airReceiverId,:name,:longitude,:latitude,:year,:style,:capacity,:pressure,:innerDiameter,:height,:thickness,:weight,:material,:structuralStyle,:railLength,:railHeight,:thickness1,:baseType,:site,:fortification,:materialType,:elasticityModulus,:yieldStress,:permissibleStress,:liquefaction6,:liquefaction7,:liquefaction8,:liquefaction9,:seismicSubsidence6,:seismicSubsidence7,:seismicSubsidence8,:seismicSubsidence9,:intensity,:weldWidth,:weldHeight,:weldStress,:damagedWeldLength,:axisRadius,:yieldStress1,:ultimateCapacity,:calculatedLength,:bottomPlateLength,:weldingWidth,:delFlag,ST_GeometryFromText(:geom,4490),:tableId,:versionId,:serialNumber,:result1,:result2,:result3,:result4,:createUser,:createTime,:updateUser,:updateTime,:extends1,:extends2,:extends3,:extends4,:extends5,:extends6,:extends7,:extends8,:extends9,:extends10)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public List<SmxAirReceiverEntity> getByVersionId(String id) {
        try {
            String sql = "select * from czt_smx_air_receiver where version_id = '" + id + "'";
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SmxAirReceiverEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void saveAirReceiverResult(SmxAirReceiverEntity it) {
        jdbcTemplate.update("update czt_smx_air_receiver set result1=?,result1_correct=?,result2=?,result2_correct=?,result3=?,result3_correct=?,result4=?,result4_correct=? where id = ?",
                it.getResult1(),it.getResult1Correct(),it.getResult2(),it.getResult2Correct(),it.getResult3(),it.getResult3Correct(),it.getResult4(),it.getResult4Correct(),it.getId());
    }

    @Override
    public SmxAirReceiverEntity getByTableId(String id) {
        try {
            String sql = "select * from czt_smx_air_receiver where table_id = '" + id + "'";
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SmxAirReceiverEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public SmxAirReceiverEntity getById(String id) {
        try {
            String sql = "select * from czt_smx_air_receiver where id = '" + id + "'";
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SmxAirReceiverEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void updateAirSupplyById(AirSupplyParam entity) {
        jdbcTemplate.update("update czt_smx_air_receiver set rail_length=?,rail_height=?,thickness1=?,weld_width=?,weld_height=?,weld_stress=?,damaged_weld_length=?,axis_radius=?,yield_stress1=?,ultimate_capacity=?,calculated_length=?,bottom_plate_length=?,welding_width=?,result1_correct=?,result2_correct=?,result3_correct=?,result4_correct=? where id =?",
                entity.getRailLength(),entity.getRailHeight(),entity.getThickness1(),entity.getWeldWidth(),entity.getWeldHeight(),entity.getWeldStress(),entity.getDamagedWeldLength(),entity.getAxisRadius(),entity.getYieldStress1(),entity.getUltimateCapacity(),entity.getCalculatedLength(),entity.getBottomPlateLength(),entity.getWeldingWidth(),entity.getResult1Correct(),entity.getResult2Correct(),entity.getResult3Correct(),entity.getResult4Correct(),entity.getId());
    }

    @Override
    public DestructiveStateVo getByBasicInfo(SmxAirReceiverEntity it,String year) {
        try {
            StringBuilder sql = new StringBuilder("select * from czt_smx_basic_library_gas where del_flag = '0' and structural_style = '" + it.getStructuralStyle() + "' and site_classification = '" + it.getSite() + "' and year = '" + year + "' and range = " + 0);
            return jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(DestructiveStateVo.class));
        } catch (DataAccessException e) {
            return new DestructiveStateVo();
        }
    }
}
